﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using ImportEnt;

namespace WebImport
{
    /// <summary>
    /// Import 的摘要说明 导入Json
    /// </summary>
    public class Import : IHttpHandler
    {
        private string tokinId = "1181250294x";

        public void ProcessRequest(System.Web.HttpContext context)
        {

            StreamReader reader = new StreamReader(context.Request.InputStream, Encoding.UTF8);

            string strJson = reader.ReadToEnd();
            if (string.IsNullOrEmpty(strJson))
            {
                context.Response.Write("JsonError");
                return;
            }

            var ent = Newtonsoft.Json.JsonConvert.DeserializeObject<DiaryEnt>(strJson);


            context.Response.ContentType = "text/plain";
            string tokin = ent.Tokin;
            if (tokin != tokinId)
            {
                context.Response.Write("TokinError");
                return;
            }
            try
            {


                string action = ent.Action;
                switch (action)
                {
                    case "addDiary":
                        string title = ent.DiaryTitle;
                        string content = ent.DiaryContent;
                        string tag = ent.DiaryTag;
                        int typeId = ent.DiaryTypeId;
                        content = HttpUtility.HtmlEncode(content);
                        var diaryService = new DiaryService();

                        Diarys data = new Diarys()
                        {
                            DiaryTitle = HttpUtility.HtmlEncode(title),
                            DiaryContent = content,
                            CreateUser = 100,
                            DiaryTypeId = typeId,
                            DiaryTag = HttpUtility.HtmlEncode(tag),
                            CreateDate = DateTime.Now
                        };

                        //验证数据是否存在
                        bool isTitleExist = diaryService.CheckTitleIsExists(data.DiaryTitle);
                        if (isTitleExist)
                        {
                            context.Response.Write("Title Exist");
                            return;
                        }


                        int dairyId = diaryService.InsertDiary(data);
                        bool isOk = dairyId > 0;

                        if (isOk)
                        {
                            context.Response.Write("OK");
                        }
                        else
                        {
                            context.Response.Write("Error");
                        }
                        break;
                    default:
                        context.Response.Write("无Action参数");
                        break;
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("Error:" + ex);
            }
        }


       

        public bool IsReusable
        {
            get { return false; }
        }
    }


}